// Andrew Hickey & Steven Steffen
// CS412 Rahimi
// Summer 2010
// Project Client program.
// Client program has two main parts, the main interface with the user
// and the listening thread that watches for updates from the server.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

// Client class to let a user play DnD with
// other users across a network.
public class Client implements Runnable {
   private Socket connection;
   private DataInputStream input;
   private DataOutputStream output;
   private BufferedReader stdin;
   private Thread outputThread;
   private PlayerChar player;
   private String myClass, readIn;
   private boolean myTurn = false;
   private JTextArea display;
   private View view = new View();

 
	public static void main(String[] args)
	{
		Client c = new Client();
	}
	
	public Client()
	{
		start();
		view.setVisible(true);
		System.out.println("Entering STDIN Listening");
		stdin = new BufferedReader(new InputStreamReader(System.in));
		while(true)
		{
			// Not sure why this is necessary, but the program wont ask for client 
			// input unless something is happending before the if statement.
			try {Thread.sleep(500);}catch(Exception e){e.printStackTrace();}
			
			if (myTurn)
			{
				
				System.out.println("Your Turn. Please enter an action: ");
				try
				{
					readIn = stdin.readLine();
					processAction(readIn);
				}catch(IOException e){e.printStackTrace();}
			}
		}
	}
   // Make connection to server and get associated streams.
   // Start separate thread to allow this applet to
   // continually update its output in text area display.
   public void start()
   {
      try {
         connection = new Socket(
            InetAddress.getByName( "127.0.0.1" ), 5000 );
         input = new DataInputStream(
                        connection.getInputStream() );
         output = new DataOutputStream(
                        connection.getOutputStream() );
      }
      catch ( IOException e ) {
         e.printStackTrace();         
      }

      outputThread = new Thread( this );
      outputThread.start();
   }

	//Method parses string sent by server to update game information
	public void parseUpdate(String s)
	{
		
	}

   // Control thread that allows continuous update of the
   // text area display.
   public void run()
   {
      // First get player's mark (X or O)
      try {
         myClass = input.readUTF();
		 System.out.println("I am the " + myClass + ". Waiting for other players to connect.");
         myTurn = ( myClass == "Dungeon Master" ? true  : false );
		 //System.out.println("Client leaving try block");
      }
      catch ( IOException e ) {
         e.printStackTrace();         
      }

      // Receive messages sent to client
      while ( true ) {
		//System.out.println("In While Loop");
         try {
            String s = input.readUTF();
            processMessage( s );
         }
         catch ( IOException e ) {
            e.printStackTrace();         
         }
      }
   }

   // Process messages sent to client
   public void processMessage( String s )
   {
	  System.out.println(s);
      if ( s.equals( "Valid move." ) ) {
		
      }
      else if ( s.equals( "Invalid move, try again" ) ) {
         myTurn = true;
      }
      else if ( s.equals( "Opponents moved" ) ) {
         try {
            String update = input.readUTF();
			System.out.println("My Turn");
            myTurn = true;
         }
         catch ( IOException e ) {
            e.printStackTrace();         
         }
      }
	  else if (s.equals("Other players connected. Your turn."))
	  {
		myTurn = true;
	  }
      else{System.out.println("Unknown Action");}
   }
   
   // Process Client Action and Sends to Server
   public void processAction(String action)
   {
      if ( myTurn )
	  {
         try {
			System.out.println("Taking Turn");
			output.writeUTF(action);
            myTurn = false;
         }
         catch ( IOException ie ) {
            ie.printStackTrace();         
         }
	  }
   }
}